package com.example.book_system.service;

import com.example.book_system.dao.BookDao;
import com.example.book_system.mapper.BookMapper;
import com.example.book_system.model.BookInfo;
import com.example.book_system.model.BookStatus;
import com.example.book_system.model.PageRequest;
import com.example.book_system.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.print.Book;
import java.util.List;

@Component
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest){
        //总记录数
        Integer count = bookMapper.queryCount();

        pageRequest.setOffset();
        List<BookInfo> bookInfos = bookMapper.queryBookInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());

        for(BookInfo book : bookInfos){
            /*if(book.getStatus() == 0){
                book.setStatusCN("无效");
            }else if(book.getStatus() == 1){
                book.setStatusCN("可借阅");
            }else{            }
            book.setStatusCN("不可借阅");
             */
            book.setStatusCN(BookStatus.getNameByCode(book.getStatus()).getName());
        }

        return new PageResult<>(bookInfos, count, pageRequest);
    }

    public void addBook(BookInfo bookInfo){
        bookMapper.addBook(bookInfo);
    }

    public BookInfo getBookById(Integer bookId){
        return bookMapper.getBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo){
        bookMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids){
        bookMapper.batchDeleteBook(ids);
    }
}
